package com.youkeyi.ddy.cloud.basic.crm.vaccination.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.vaccination.entity.SysVaccinationDetails;
import com.youkeyi.ddy.cloud.basic.crm.vaccination.service.SysVaccinationDetailsService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.CrmVaccinationDetailsCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.dto.CrmVaccinationDetailsListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.dto.CrmVaccinationDetailsListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.param.CrmVaccinationDetailsCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.param.CrmVaccinationDetailsDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.param.CrmVaccinationDetailsQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.param.CrmVaccinationDetailsUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 疫苗接种明细 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-03
 */
@Service
public class SysVaccinationDetailsController implements CrmVaccinationDetailsCloudService {
    
    @Autowired
    private SysVaccinationDetailsService sysVaccinationDetailsService;

    @Override
    public CrmVaccinationDetailsListCloudDto query(CrmVaccinationDetailsQueryCloudParam param) {
        CrmVaccinationDetailsListCloudDto dto = new CrmVaccinationDetailsListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getVaccinationDetailsPrimaryKeys())) {
            param.getVaccinationDetailsPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getOrderKeys())) {
            param.getOrderKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getGoodsKeys())) {
            param.getGoodsKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysVaccinationDetails> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getVaccinationDetailsPrimaryKeys()), SysVaccinationDetails::getPrimaryKey, param.getVaccinationDetailsPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysVaccinationDetails::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getOrderKeys()), SysVaccinationDetails::getOrderKey, param.getOrderKeys())
                .in(CollectionUtils.isNotEmpty(param.getGoodsKeys()), SysVaccinationDetails::getGoodsKey, param.getGoodsKeys())
                .ne(SysVaccinationDetails::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysVaccinationDetails> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysVaccinationDetails> vaccinationDetailsList = sysVaccinationDetailsService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(vaccinationDetailsList.getRecords())) {
            dto.setTotal(vaccinationDetailsList.getTotal());

            List<CrmVaccinationDetailsListItemCloudDto> items = new ArrayList<>();
            vaccinationDetailsList.getRecords().forEach(vaccinationDetails -> {
                CrmVaccinationDetailsListItemCloudDto item = new CrmVaccinationDetailsListItemCloudDto();
                item.setVaccinationDetailsPrimaryKey(vaccinationDetails.getPrimaryKey());
                item.setCorpKey(vaccinationDetails.getCorpKey());
                item.setOrderKey(vaccinationDetails.getOrderKey());
                item.setGoodsKey(vaccinationDetails.getGoodsKey());
                item.setGoodsName(vaccinationDetails.getGoodsName());
                item.setVaccineDoses(vaccinationDetails.getVaccineDoses());
                item.setNumberOfStitches(vaccinationDetails.getNumberOfStitches());
                item.setDoctorKey(vaccinationDetails.getDoctorKey());
                item.setDoctorName(vaccinationDetails.getDoctorName());
                item.setRemark(vaccinationDetails.getRemark());
                item.setStatus(vaccinationDetails.getStatus());
                item.setIsDeleted(vaccinationDetails.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(vaccinationDetails.getCreateTime() != null ? vaccinationDetails.getCreateTime().getTime() : null);
                item.setCreateUserKey(vaccinationDetails.getCreateUserKey());
                item.setCreateUserName(vaccinationDetails.getCreateUserName());
                item.setUpdateTime(vaccinationDetails.getUpdateTime() != null ? vaccinationDetails.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(vaccinationDetails.getUpdateUserKey());
                item.setUpdateUserName(vaccinationDetails.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmVaccinationDetailsCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getOrderKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysVaccinationDetails vaccinationDetails = new SysVaccinationDetails();
        vaccinationDetails.setPrimaryKey(SimpleUUidUtils.getUuid());
        vaccinationDetails.setCorpKey(param.getCorpKey());
        vaccinationDetails.setOrderKey(param.getOrderKey());
        vaccinationDetails.setGoodsKey(param.getGoodsKey());
        vaccinationDetails.setGoodsName(param.getGoodsName());
        vaccinationDetails.setVaccineDoses(param.getVaccineDoses());
        vaccinationDetails.setNumberOfStitches(param.getNumberOfStitches());
        vaccinationDetails.setDoctorKey(param.getDoctorKey());
        vaccinationDetails.setDoctorName(param.getDoctorName());
        vaccinationDetails.setRemark(param.getRemark());
        vaccinationDetails.setStatus(param.getStatus());
        vaccinationDetails.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        vaccinationDetails.setCreateUserKey(param.getCreateUserKey());
        vaccinationDetails.setCreateUserName(param.getCreateUserName());
        vaccinationDetails.setCreateTime(new Date());
        boolean save = sysVaccinationDetailsService.save(vaccinationDetails);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(vaccinationDetails.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmVaccinationDetailsUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getVaccinationDetailsPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysVaccinationDetails> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysVaccinationDetails::getPrimaryKey, param.getVaccinationDetailsPrimaryKey());
        SysVaccinationDetails vaccinationDetails = new SysVaccinationDetails();
        vaccinationDetails.setPrimaryKey(param.getVaccinationDetailsPrimaryKey());
        vaccinationDetails.setVaccineDoses(param.getVaccineDoses());
        vaccinationDetails.setNumberOfStitches(param.getNumberOfStitches());
        vaccinationDetails.setDoctorKey(param.getDoctorKey());
        vaccinationDetails.setDoctorName(param.getDoctorName());
        vaccinationDetails.setRemark(param.getRemark());
        vaccinationDetails.setStatus(param.getStatus());
        vaccinationDetails.setUpdateUserKey(param.getUpdateUserKey());
        vaccinationDetails.setUpdateUserName(param.getUpdateUserName());
        vaccinationDetails.setUpdateTime(new Date());
        boolean save = sysVaccinationDetailsService.update(vaccinationDetails, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmVaccinationDetailsDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getVaccinationDetailsPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysVaccinationDetails> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysVaccinationDetails::getPrimaryKey, param.getVaccinationDetailsPrimaryKeys());

        SysVaccinationDetails vaccinationDetails = new SysVaccinationDetails();
        vaccinationDetails.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        vaccinationDetails.setUpdateUserKey(param.getUpdateUserKey());
        vaccinationDetails.setUpdateUserName(param.getUpdateUserName());
        vaccinationDetails.setUpdateTime(new Date());
        boolean save = sysVaccinationDetailsService.update(vaccinationDetails, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
